package com.ifyou.controller;

import cn.hutool.core.io.FileUtil;
import com.ifyou.config.TestConfig;
import org.nd4j.shade.guava.net.MediaType;
import org.springframework.core.io.UrlResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;

@RestController
public class TestController {

    @Resource
    private TestConfig testConfig;

    @GetMapping("/home")
    public void test() {
        System.out.println(testConfig);
    }

    @GetMapping(value = "/view")
    public void minioView(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Base64.Encoder encoder = Base64.getEncoder();

        File file = new File("C:\\Users\\gw00224518\\Downloads\\login-bg.svg");

        byte[] bytes = FileUtil.readBytes(file);

        String encodeToString = encoder.encodeToString(bytes);

        response.setContentType("image/svg+xml");

        try (
                OutputStream outputStream = response.getOutputStream()
        ) {
            outputStream.write(encodeToString.getBytes());
            response.flushBuffer();
        }
    }

    @GetMapping(value = "/view2")
    public void minioView2(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("image/svg+xml");
        try (InputStream inputStream = Files.newInputStream(new File("C:\\Users\\gw00224518\\Downloads\\login-bg.svg").toPath());
             OutputStream outputStream = response.getOutputStream()
        ) {
            byte[] buf = new byte[1024];
            int len;
            while ((len = inputStream.read(buf)) > 0) {
                outputStream.write(buf, 0, len);
            }
            response.flushBuffer();
        }
    }

//    @GetMapping("/svg/preview")
//    public ResponseEntity<Resource> getSvgPreview() {
//        Path svgStoragePath = Paths.get("path/to/your/svg/files");
//        try {
//            Path filePath = svgStoragePath.resolve("example.svg");
//            org.springframework.core.io.Resource resource = new UrlResource(filePath.toUri());
//
//            if (resource.exists() || resource.isReadable()) {
//                return ResponseEntity.ok()
//                        .contentType(MediaType.IMAGE_SVG_XML)
//                        .header("Content-Disposition", "inline; filename=example.svg")
//                        .body(resource);
//            } else {
//                throw new RuntimeException("Could not read the SVG file.");
//            }
//        } catch (Exception e) {
//            throw new RuntimeException("Error handling SVG request", e);
//        }
//    }

}
